package com.powerbosframework.web.servlet;
		

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.powerbosframework.context.ApplicationContext;
import com.powerbosframework.log.LogFactory;
import com.powerbosframework.log.Logger;
import com.powerbosframework.web.action.ActionException;
import com.powerbosframework.web.action.RequestProcessor;
/**
 * 控制器类
 * 
 * @author youfu.wang
 * @version 1.0
 */
public class ActionServlet extends HttpServlet {
	protected ServletContext servletContext = null;
	private static Logger log = LogFactory.getInstance();
	
	public ActionServlet(){
		
	}
	/**
	 * 销毁控制器所占内存
	 */
	public void destroy() {

	}
	public void init(){		
		this.servletContext=super.getServletContext();
	}
	/**
	 * 处理GET请求方法
	 * 
	 * @param httpservletrequest
	 * @param httpservletresponse
	 * @throws IOException,
	 *             ServletException
	 */
	public void doGet(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws IOException,
			ServletException {
		try {
			process(httpservletrequest, httpservletresponse);
		} catch (ActionException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理POST请求方法
	 * 
	 * @param httpservletrequest
	 * @param httpservletresponse
	 * @throws IOException,
	 *             ServletException
	 */
	public void doPost(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws IOException,
			ServletException {
		try {
			process(httpservletrequest, httpservletresponse);
		} catch (ActionException e) {
			e.printStackTrace();
		}
	}

	private synchronized void process(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws ActionException {
		
		RequestProcessor requestprocessor=getProcessorForConfig();
		if(requestprocessor==null){		
			requestprocessor=this.getRequestProcessor();
		}
		try {
			requestprocessor.process(httpservletrequest, httpservletresponse);
		} catch (Exception e) {			
			e.printStackTrace();
		} catch (ActionException e) {			
			e.printStackTrace();
		}
	}
	private synchronized RequestProcessor getRequestProcessor(){
		RequestProcessor requestprocessor = getProcessorForConfig();
		if(requestprocessor==null){			
			requestprocessor=RequestProcessor.getRequestProcessor();						
		}
		try {
			String s = "com.powerbosframework.web.action.WebApplicationContextServlet";
			requestprocessor.init((ApplicationContext)servletContext.getAttribute(s),this);
		} catch (ServletException e) {			
			e.printStackTrace();
		}
		String s = "com.powerbosframework.web.action.RequestProcessor";
		servletContext.setAttribute(s, requestprocessor);
        return requestprocessor;
	}
	private synchronized RequestProcessor getProcessorForConfig(){
		String s = "com.powerbosframework.web.action.RequestProcessor";
		return (RequestProcessor)servletContext.getAttribute(s);
	}
	static Class class$(String s) {
		try {
			return Class.forName(s);
		} catch (ClassNotFoundException classnotfoundexception) {
			throw new NoClassDefFoundError(classnotfoundexception.getMessage());
		}
	}

}
